package utl.mofen.easyui;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.sql.Date;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import utl.mofen.utils.Base64;


public class Convert {

	private static String nulltoJson() {
		return "";
	}

	private static String stringToJson(String s) {
		if (s == null)
			return nulltoJson();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			char ch = s.charAt(i);
			switch (ch) {
			case '"':
				sb.append("\\\"");
				break;
			case '\\':
				sb.append("\\\\");
				break;
			case '\b':
				sb.append("\\b");
				break;
			case '\f':
				sb.append("\\f");
				break;
			case '\n':
				sb.append("\\n");
				break;
			case '\r':
				sb.append("\\r");
				break;
			case '\t':
				sb.append("\\t");
				break;
			case '/':
				sb.append("\\/");
				break;
			default:
				if ((ch >= 0) && (ch <= '\037')) {
					String ss = Integer.toHexString(ch);
					sb.append("\\u");
					for (int k = 0; k < 4 - ss.length(); k++) {
						sb.append('0');
					}
					sb.append(ss.toUpperCase());
				} else {
					sb.append(ch);
				}
			}
		}
		return sb.toString();
	}
	public static ArrayList getPageRecords(ResultSet rs, int _pageSize, int _currentPage) 
	{
		
		PageBean page = new PageBean();
		try {
			rs.last();
			int countRecord = rs.getRow();
			page.setAll(countRecord,_pageSize);
			page.setCurrentPage(_currentPage);
			
			int currentRecord = (page.getCurrentPage() - 1)	* page.getSizePage();
			if (currentRecord == 0)
				rs.beforeFirst();
			else
				rs.absolute(currentRecord);//定位到当前页的第一行			
			
			return getRecords(rs, _pageSize);
		} catch (Exception e) {
			System.out.println("error" + e.getMessage());
			return null;
		}
	}

	public static ArrayList getRecords(ResultSet rs, int _getNum) 
	{		
		ArrayList list = new ArrayList();
		try {
			int _count = 1;
			
			ResultSetMetaData rsmd = rs.getMetaData();
			String columnName = "";
			Object columnValue = null;
			int columntype = 0;
			
			String array = "";
			String tempStr = "";
			while (rs.next()) {
				
				if ((_count>_getNum) && (_getNum>0))  //超过记录数
				{
					break;
				}
				
				array = "{";				
				for (int j = 1; j <= rsmd.getColumnCount(); j++) {
					columnName = rsmd.getColumnName(j);
					columntype = rsmd.getColumnType(j);
					columnValue = rs.getObject(columnName);
					array = array + "\"" + columnName.toLowerCase() + "\"";
					array = array + ":";
					if (columnValue != null) {
						switch (columntype) {
						case Types.TIMESTAMP:
							Timestamp dt = rs.getTimestamp(j);
							SimpleDateFormat datef = new SimpleDateFormat(
									"yyyy-MM-dd HH:mm:ss");
							tempStr = datef.format(dt);
							array = array + "\"" + tempStr + "\"";
							break;
						case Types.DATE:
							Date now = rs.getDate(columnName);
							DateFormat format1 = new SimpleDateFormat(
									"yyyy-MM-dd");
							String zzz = format1.format(now);
							array = array + "\"" + zzz + "\"";
							System.out.println(array);
							break;
						case Types.CHAR:
						case Types.VARCHAR:
							String zz = rs.getString(columnName);
							zz = stringToJson(zz);
							array = array + "\"" + zz + "\"";
							break;
						case 3:
							DecimalFormat df = new DecimalFormat("0.0000");
							if (columnValue != null) {
								array = array + "\"" + df.format(columnValue)
										+ "\"";
							} else {
								array = array + "\"" + "0.0" + "\"";
							}
							break;
						default:
							tempStr = rs.getString(j);
							tempStr = stringToJson(tempStr);
							array = array + "\"" + tempStr + "\"";
		
							System.out.print("处理Default:" + columnName + " --->");
							System.out.println(columntype);
							break;
						}
					} else
					{
						array = array + "\" \"";
					}
					if (j < rsmd.getColumnCount())
					{
						array = array + ",";
					}
				}
				array = array + "}";
				list.add(array);				
				_count = _count + 1;
				
			} // end while
			return list;
		} catch (Exception localException) {
			System.out.println("error" + localException.getMessage());
			return null;
		}
	}

}
